/**
 * 圈子升级-申请页面
 * @Author : woon
 * @Timestamp : 2017-1-09
 */

var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var root = ui("$");
var circleUrl = require("url/circle");
var http = require("helper/http");
var httpSilence = require("helper/httpSilence");
var openHelper = require("helper/open");

var circleUpgradeUrl = circleUrl.url.upgradeApply.apply2Upgrade;//申请升级

//andriod返回键
page.on("back", function() {
    app.closePage();
});
var circleId = null;
var cityId = null;//城市Id
var fromZoneLevel = null;
var toZoneLevel = null;

var toZoneLevelList = null;
var toCityId = null;
var toExtCommunityIds = null;
var excludeCommunityIds = "";
page.on("loaded", function(){
	circleId = page.getData().circleId;
	loadCircleInfo();
	
});

ui("ALayout_root").on("touch", function(data, e) {
	page.hideKeyboard();
})
//选择社区、城市 返回
page.on("result", function(data){
	if(data.cityId){
		toCityId = data.cityId;
		ui("lableCityName").text = data.cityName;
	}else if(data.type == "selectCommunitys"){
		toExtCommunityIds = data.communityIds;
		ui("lableCommName").text = data.communityNames;
	}
});

//选择城市
ui("do_ALayout_city").on("touch", function() {
	openHelper.open("source://view/area/cityMain.ui");
});

//选择社区
ui("do_ALayout_com").on("touch", function() {
	openHelper.open("source://view/circle/base/upgrade/selectCommunity.ui",{"excludeCommunityIds":excludeCommunityIds});
});

/****************处理页面-头部-begin*****************/
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
ui(addheader+".labelTitle").text = "圈子升级申请";
//左上角-返回
ui(addheader+".headerLeft").on("touch", function() {
    app.closePage()
});
/****************处理页面-头部-end*****************/

function loadCircleInfo() {
	if(circleId == null){
		nf.toast("圈子id为空");
		return;
	}
	http.post(circleUrl.url.base.detail4upgrade,{id:circleId},function(data){
		var circleBase = data.circleBase;
		toZoneLevelList = data.toZoneLevelList;//可选择的区域范围列表
		if(circleBase!=null){
			cityId = circleBase.cityId;
			fromZoneLevel = circleBase.fromZoneLevel;
			ui("labelFromZone").text = circleBase.zoneLevelName+"升级";
			setExcludeCommunityIds(circleBase);
		}
	});
}

//排除原来的社区ID及已经扩展的社区IDS
function setExcludeCommunityIds(circleBaseVo){
	var oldComId = circleBaseVo.communityId;
	var extComIds = circleBaseVo.extCommunityIds;
	if(oldComId && oldComId != null && oldComId != undefined){
		excludeCommunityIds = oldComId;
	}
	if(extComIds != null && extComIds != "undefined" && extComIds != ""){
		if(excludeCommunityIds==""){
			excludeCommunityIds = extComIds;
		}else{
			excludeCommunityIds = excludeCommunityIds+";"+extComIds;
		}
		
	}
}

//5秒防止多次点击
ui("btnCommit").on("touch", "", 5000, function() {
	page.hideKeyboard();
	submitUpgradeApply();
})

function submitUpgradeApply(){
	var applyContent = ui("do_TextBox_content").text;
	if(applyContent==""){
		nf.toast("请输入升级理由!");
		return;
	}
	if(applyContent.length>100){
		nf.toast("升级理由长度限制为100个文字");
		return;
	}
	if(toZoneLevel==null || toZoneLevel==""){
		nf.toast("请选择升级范围!");
		return;
	}else if(toZoneLevel==2){//多社区
		toCityId = null;
		ui("lableCityName").text = "";
		if(toExtCommunityIds == null || toExtCommunityIds==""){
			nf.toast("请选择扩展社区!");
			return;
		}
	}else if(toZoneLevel==3){//城市
		toExtCommunityIds = null;
		ui("lableCommName").text = "";
		if(toCityId == null || toCityId==""){
			nf.toast("请选择城市!");
			return;
		}
	}
	var params = {
			circleId:circleId,
			fromZoneLevel: fromZoneLevel,
			toZoneLevel:toZoneLevel,
			applyContent: applyContent
	}
	if(toCityId != null && toCityId!=""){
		params.toCityId = toCityId;
	}
	if(toExtCommunityIds != null && toExtCommunityIds!=""){
		params.toExtCommunityIds = toExtCommunityIds;
	}
	http.post(circleUpgradeUrl,params,function(data){
		nf.toast("圈子升级申请提交成功,请等待审核.");
		app.closePage({"circle_upgrade":true});
	});
}


//选择升级后的区域 --begin
var addPicker = ui("ALayout_root").add("picker","source://view/reuse/form/oneCategory.ui", 0, 0);
var picker = ui("picker");
ui("do_ALayout_toZone").on("touch", "", 300, function() {
	var ptitle = {"title" : "选择升级区域范围","id" : "zoneLevelSelect"};
	if(toZoneLevelList!=null && toZoneLevelList.length>0){
		var pcount = [ ptitle, toZoneLevelList ];
		page.fire("oneCategoryList", pcount);
		page.fire("oneCategory", "true");
	}
});
page.on("refreshOneCategory", function(data) {
	if ("zoneLevelSelect" == data[1]) {
		toZoneLevel = data[0].oneid;
		ui("labelToZone").text = data[0].onename;
		setShowCityCommunity();//显示【选择社区或城市】
	}
});
//选择升级后的区域 --end

function setShowCityCommunity() {
	if(toZoneLevel == 2){//升级为【多个社区】
		ui("do_ALayout_com").visible = true;
		ui("do_ALayout_city").visible = false;
	}else if(toZoneLevel == 3){//升级为【城市】
		ui("do_ALayout_com").visible = false;
		ui("do_ALayout_city").visible = true;
	}else if(toZoneLevel == 4){//升级为【全国】
		ui("do_ALayout_com").visible = false;
		ui("do_ALayout_city").visible = false;
	}
	ui("do_ScrollView_1").redraw();
}
